package com.hspedu.houserent.service;

import com.hspedu.houserent.house.House;

/**
 * (业务层)
 * HouseService.java
 * 1.响应houseview的调用
 * 2.完成对房屋信息的各种操作(
 * 增删改查
 * c(create)  r(read)
 * u(update) d(delete))
 * 3.定义House[]，保存House对象
 */
public class HouseService {

    private House[] houses;//用于存放House对象的
    private int houseNums = 1;//记录当前有多少个房屋信息
    private int idCounter = 1;


    public HouseService(int size) {
        houses = new House[size];//当创建HouseService对象，指定数组的大小
        houses[0] = new House(1, "jack", "123", "武侯区", 1500, "已出租");
    }

    //list方法，返回houses
    public House[] list() {
        return houses;
    }

    //查找Houses对象数组中，id值为 findId的对象下标，默认为-1
    public int search(int findId) {
        int index = -1;
        for (int i = 0; i < houseNums ; i++) {
            if (findId == houses[i].getId()){
                index = i;
                break;
            }
        }
        return index;
    }

    //read 用于查找房屋信息，如果有，则打印房屋信息，如果没有返回true
    public House read(int searchId) {
    /*    for (int i = 0; i < houseNums; i++) {
            if (searchId == houses[i].getId()) {
                return houses[i];
            }
        }
        return null;*/
        int index = search(searchId);
        if (index == -1){
            return null;
        }
        return houses[index];
    }


    //update方法，通过传进来的房屋id，对相应id的房屋信息进行修改
    public boolean update(int updateId, House newHouse) {
        int index =search(updateId);
        if (index == -1){
            return  false;
        }

        houses[index].setName(newHouse.getName());
        houses[index].setPhone(newHouse.getPhone());
        houses[index].setRet(newHouse.getRet());
        houses[index].setAddress(newHouse.getAddress());
        houses[index].setState(newHouse.getState());
        return true;
    }


    //del方法，查找房屋id并进行删除，返回值boolean
    public boolean del(int delId) {
        if (houseNums == 0) {
            System.out.println("没有该房屋信息！");
            return false;
        }

        int index = search(delId);
        if (index == -1) {
            System.out.println("房屋编号，不存在！");
            return false;
        }

        //如果找到，把删除元素 的后面的所有元素往前移一位，并置空
        for (int i = index; i < houseNums - 1; i++) {
            houses[i] = houses[i + 1];
            houses[i + 1].setId(houses[i + 1].getRet() - 1);
        }
        //把当前数组最后一个元素置空
        houses[--houseNums] = null;
        idCounter--;
        /*houses[houseNums-1] = null;
        houseNums--;*/
        return true;
    }

    //add方法，添加新的对象到house数组中，并返回boolean值
    public boolean add(House newHouse) {
        //如果当前数量大于等于数组的长度，已满就不能添加，直接返回false
        House[] addHouse = new House[houses.length * 2];
        if (houseNums == houses.length) {
            //进行扩容
            for (int i = 0; i < houses.length; i++) {
                addHouse[i] = houses[i];
            }
            houses = addHouse;
        }
        //把newHouse 加入到最后一个元素后面的那个位置上
        houses[houseNums++] = newHouse;
        //需要设计一个id自增长的记录，然后更新newHouse的id
        newHouse.setId(++idCounter);
        return true;
    }
}
